<script setup>
import {
  listDept,
  updateDept,
  addDept,
  delDept,
  getDeptInfo,
  changeDeptState,
} from "@/api/hospital";
const { proxy } = getCurrentInstance();
const loading = ref(true);
const list = ref([]);
const open = ref(false);
const title = ref("");

// 查询参数
const data = reactive({
  form: {},
  rules: {
    name: [
      { required: true, message: "科室名称不能为空", trigger: "blur" },
      {
        min: 2,
        max: 20,
        message: "科室名称长度必须介于 2 和 20 之间",
        trigger: "blur",
      },
    ],
  },
});

const { form, rules } = toRefs(data);

/** 查询角色列表 */
function getList() {
  loading.value = true;
  listDept().then((res) => {
    loading.value = false;
    list.value = res.data;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "新增科室信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const deptId = row.id;
  getDeptInfo(deptId).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改科室信息";
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const deptName = row.name || names.value;
  const deptId = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除科室名称为"' + deptName + '"的数据项？')
    .then(function () {
      return delDept(deptId);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除科室成功");
    })
    .catch(() => {});
}

/** 状态切换 */
function handleStatusChange(row) {
  let text = row.state === "1" ? "启用" : "停用";
  proxy.$modal
    .confirm("确认要" + text + '科室名称为"' + row.name + '"的科室吗?')
    .then(function () {
      return changeDeptState(row.id, row.state);
    })
    .then(() => {
      proxy.$modal.msgSuccess(text + "成功");
    })
    .catch(function () {
      row.state = row.state === "0" ? "1" : "0";
    });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["deptRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        updateDept(form.value).then((response) => {
          if (response.code == 200) proxy.$modal.msgSuccess("修改科室信息成功");
          else proxy.$modal.alertError(response.msg);
          open.value = false;
          getList();
        });
      } else {
        addDept(form.value).then((response) => {
          if (response.code == 200) proxy.$modal.msgSuccess("新增成功");
          else proxy.$modal.alertError(response.msg);
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 重置操作表单 */
function reset() {
  form.value = {
    id: undefined,
    name: undefined,
  };
  proxy.resetForm("deptRef");
}

getList();
</script>

<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          >新增</el-button
        >
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="list">
      <el-table-column type="index" label="序号" width="50" />
      <el-table-column label="科室名称" align="center" key="name" prop="name" />
      <el-table-column label="状态" align="center" key="state">
        <template #default="scope">
          <el-switch
            v-model="scope.row.state"
            active-value="1"
            inactive-value="0"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="150">
        <template #default="scope">
          <el-tooltip content="修改" placement="top">
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleUpdate(scope.row)"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改科室配置对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form :model="form" :rules="rules" ref="deptRef" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="科室名称" prop="name">
              <el-input
                v-model="form.name"
                placeholder="科室名称"
                maxlength="30"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped></style>
